package com.fare.taluo.web.action;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.JSONUtils;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fare.taluo.BaseFdcAction;
import com.fare.taluo.application.crawl.TaskAdapterApplication;
import com.fare.taluo.application.plane.channel.SourceApplication;
import com.fare.taluo.util.DateJsonValueProcessor;
import com.fare.taluo.vo.core.SourceEntityVO;
import com.fare.taluo.vo.core.TaskAdapterEntityVO;

/**
 * 
 * @author linzhenlie
 *
 * 2012-12-11
 */
@Controller("taskAdapterAction")
@Scope("prototype")

public class TaskAdapterAction extends BaseFdcAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = -3561554712963828385L;
	@Autowired
	private TaskAdapterApplication taskAdapterApplication;
	@Autowired
	private SourceApplication sourceApplication;
	//适配器Id
	private Long adpId;
	//渠道名称
	private Long sourceId;
	//上传类的ID
	private Long adapterUploadIds;
	//创建日期
	private Date creatTime;
	//适配器名称
	private String adapterName;
	//适配器路径和类名
	private String adapterClass;
	
	//返回页面的json格式数据
	private String jsonText;
	
	//适配器状态
	private String isEnable;
	//接收页面的json格式数据
	private String jspData;
	
	private TaskAdapterEntityVO taskAdapterEntityVO;
	
	private String flag;
	
//	public String dealSomething() throws UnsupportedEncodingException{
//		vo.setSourceName(new String(vo.getSourceName().getBytes("ISO-8859-1"),"UTF-8"));
////		vo.setToCityName(new String(vo.getToCityName().getBytes("ISO-8859-1"),"UTF-8"));
//		if("save".equals(flag)||"update".equals(flag)){
//			TaskAdapterApplication.saveOrUpdate(vo);
//		}else if("delete".equals(flag)){
//			freightPublishApplication.remove(vo.getId());
//		}
//		returnPageURL = "/fare/_zqspqsz.jsp";
//		return "dispatcher";
//	}
	
	public String toJsp() throws Exception{
		returnPageURL="/fare/_zqspqsz.jsp";
		return "dispatcher";
	}
	//初始化渠道网站名称下拉列表，查找所有
	public void init() throws Exception{
		List<SourceEntityVO> list=sourceApplication.findAllSourceEntity();
		List<SourceEntityVO> jspList=new ArrayList<SourceEntityVO>();
		for (SourceEntityVO sourceEntityVO : list) {
			SourceEntityVO vo=new SourceEntityVO();
			vo.setId(sourceEntityVO.getId());
			vo.setName(sourceEntityVO.getName());
			jspList.add(vo);
		}
		
		
		if(jspList!=null){//查找到数据
			if(jspList.size()>0){
				JSONArray jsarry=JSONArray.fromObject(jspList);
				jsonText=jsarry.toString();
				//System.out.println(jsonText);
			}
		}
		 HttpServletResponse response=ServletActionContext.getResponse();
		 response.setContentType("text/html;charset=utf-8"); 
		 response.getWriter().write(jsonText);
	}
	//修改modify
	public String modify()throws Exception{
		
		taskAdapterEntityVO.setId(adpId);
		taskAdapterEntityVO.setCreattime(creatTime);
		taskAdapterEntityVO.setSourceID(sourceId);
		taskAdapterEntityVO.setAdapterUploadId(adapterUploadIds);
		taskAdapterApplication.saveM(taskAdapterEntityVO);
//		if(vo!=null){
//		getTaskAdapterApplication().saveOrUpdateTaskAdapterEntity(vo);
//		}
		
		returnPageURL = "/taluo-web/fare/_zqspqsz.jsp";
			return null ;
	}
	
	
	//增加适配器配置
	public String addOrEditTaskAdapter() throws Exception {
		
		JSONUtils.getMorpherRegistry().registerMorpher(  
		 		new DateMorpher(new String[] {"yyyy-MM-dd HH:mm:ss"}));
		System.out.println("jspData++++++++"+jspData);
		JSONObject json=JSONObject.fromObject(jspData);
		System.out.println("jspData"+jspData);
		json.remove("updatetime");
		System.out.println("获得数据："+json);
		
		TaskAdapterEntityVO adapterVO=(TaskAdapterEntityVO)
				JSONObject.toBean(json,TaskAdapterEntityVO.class);
		System.out.println("adapterVO++"+adapterVO);
		if(adapterVO!=null){
			getTaskAdapterApplication().saveOrUpdateTaskAdapterEntity(adapterVO);
		}
		return null;
	}
	
	//删除适配器配置-linzhenlie
	public String deleteTaskAdapter() throws Exception {
		JSONObject jsonObject = JSONObject.fromObject(jspData);
		TaskAdapterEntityVO adapterVO=(TaskAdapterEntityVO)
				jsonObject.toBean(jsonObject, TaskAdapterEntityVO.class);
		if(adapterVO!=null){
			getTaskAdapterApplication().deleteTaskAdapterEntity(adapterVO.getId());
			System.out.println("are you delete");
			getTaskAdapterApplication().deleteAdapterUploadedEntity(adapterVO.getAdapterUploadId());
			System.out.println("yes!");
		}
		
		return null;
	}
	
	//查找适配器配置显示列表-linzhenlie
	public void showTaskAdapterList() throws Exception {
		HttpServletResponse response=ServletActionContext.getResponse();
//		PrintWriter out = response.getWriter();
		
		adapterName=URLDecoder.decode(adapterName,"utf-8");
		adapterClass=URLDecoder.decode(adapterClass,"utf-8");
		isEnable=URLDecoder.decode(isEnable,"utf-8");
		String a="999";
//		System.out.println(adapterName);
//		System.out.println(adapterClass);
//		System.out.println("++++++isEnable:"+isEnable);
//		System.out.println("+++++++++showIt:"+isEnable.equals(a));
		List<TaskAdapterEntityVO> list = new ArrayList<TaskAdapterEntityVO>();
		if(sourceId == 99 && isEnable.equals(a)==true){
			list=getTaskAdapterApplication().
					findTaskAdapterByConsdions(null, null,adapterName, adapterClass);
		}else if(sourceId == 99 &&(isEnable.equals(a))==false){
			list=getTaskAdapterApplication().
					findTaskAdapterByConsdions(null, isEnable,adapterName, adapterClass);
		}else if(sourceId != 99 &&(isEnable.equals(a))==true){
			list=getTaskAdapterApplication().
					findTaskAdapterByConsdions(sourceId+"", null,adapterName, adapterClass);
		}else{
			list=getTaskAdapterApplication().
					findTaskAdapterByConsdions(sourceId+"", isEnable,adapterName, adapterClass);
		}
				JsonConfig jsonConfig = new JsonConfig();
				jsonConfig.registerJsonValueProcessor(Date.class,new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));
				JSONArray jsarry=JSONArray.fromObject(list,jsonConfig);
				jsonText="{Rows:"+jsarry.toString()+"}";
				System.out.println(jsonText);
				    response.setContentType("text/html;charset=utf-8"); 
					response.getWriter().write(jsonText);
				
	}

	
	public TaskAdapterApplication getTaskAdapterApplication() {
		return taskAdapterApplication;
	}

	public void setTaskAdapterApplication(
			TaskAdapterApplication taskAdapterApplication) {
		this.taskAdapterApplication = taskAdapterApplication;
	}

	public Long getSourceId() {
		return sourceId;
	}
	public void setSourceId(Long sourceId) {
		this.sourceId = sourceId;
	}

	public String getAdapterName() {
		return adapterName;
	}

	public void setAdapterName(String adapterName) {
		this.adapterName = adapterName;
	}

	public String getAdapterClass() {
		return adapterClass;
	}

	public void setAdapterClass(String adapterClass) {
		this.adapterClass = adapterClass;
	}

	public String getJsonText() {
		return jsonText;
	}

	public void setJsonText(String jsonText) {
		this.jsonText = jsonText;
	}

	public String getJspData() {
		return jspData;
	}

	public void setJspData(String jspData) {
		this.jspData = jspData;
	}

	public SourceApplication getSourceApplication() {
		return sourceApplication;
	}

	public void setSourceApplication(SourceApplication sourceApplication) {
		this.sourceApplication = sourceApplication;
	}
	
	public Long getAdpId() {
		return adpId;
	}
	public void setAdpId(Long adpId) {
		this.adpId = adpId;
	}
	public TaskAdapterEntityVO getTaskAdapterEntityVO() {
		return taskAdapterEntityVO;
	}
	public void setTaskAdapterEntityVO(TaskAdapterEntityVO taskAdapterEntityVO) {
		this.taskAdapterEntityVO = taskAdapterEntityVO;
	}
	public Date getCreatTime() {
		return creatTime;
	}
	public void setCreatTime(Date creatTime) {
		this.creatTime = creatTime;
	}
	public Long getAdapterUploadIds() {
		return adapterUploadIds;
	}
	public void setAdapterUploadIds(Long adapterUploadIds) {
		this.adapterUploadIds = adapterUploadIds;
	}
	public String getIsEnable() {
		return isEnable;
	}
	public void setIsEnable(String isEnable) {
		this.isEnable = isEnable;
	}
	
	
	
}
